package lxt.action;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import lxt.action.core.ActionSupport;
import lxt.entity.UserInfo;
import lxt.service.UserService;
/**
 * 用户管理action
 * @author Lit
 *
 */
public class UserAction extends ActionSupport{

	private static final long serialVersionUID = 1L;
	//定义属性
	private String username;
	private String password;
	private Integer sex;
	private String yobbo;
	private String phone;
	private Integer loginType;
	private Date lastLogin;
	//定义service
	private UserService userService;
	 //定义对象
	private List<UserInfo> users;
	
	private String deleteIds;
	// 定义分页变量
	private Integer pageCounts = 10; //每页显示数量
	private String currPageStr = "1"; //分页
	private Integer totalCounts ;//总数据量
	
	/**
	 * 加载
	 * @return
	 */
	public String loadAll(){
		try{
			//定义从第几条显示
			int offset =( Integer.parseInt(currPageStr)-1)*pageCounts;
			//获得驾驶员列表
			users = userService.loadUser(username, offset, pageCounts);
			//年龄计算
			if(users.size()>0){
				//日期
				Calendar ca = Calendar.getInstance();
				ca.setTime(new Date());
				int TodayY = ca.get(Calendar.YEAR);
				for(int i=0;i<users.size();i++){
					String DriverY = users.get(i).getYobbo();
					if(DriverY!=null&&!DriverY.equals("")){
					int year =TodayY - Integer.parseInt(DriverY);
					users.get(i).setYobbo(String.valueOf(year));
					}
				}
			}
			//获取总的记录数
			totalCounts = userService.loadAllUserCount(username);
		}catch(Exception ex){
			ex.printStackTrace();
			this.addActionMessage("操作失败，请检查重新操作");
		}
		return SUCCESS;
	}
	/**
	 * 添加预处理
	 * @return
	 */
	public String AddPre(){
		try{
			
		}catch(Exception ex){
			ex.printStackTrace();
			this.addActionMessage("操作失败，请检查重新操作");
		}
		return SUCCESS;
	}
	/**
	 * 添加
	 */
	public String add(){
		try{
			//定义变量
			UserInfo user ;
			user = this.userService.loadUserById(username);
			if(user!=null){
				this.addActionMessage("该用户已经存在，请重新填写");
				return INPUT;
			}
			else{
				user = new UserInfo();
				//user.setLastLogin(lastLogin);
				user.setLoginType(loginType);
				user.setPassword("123456");
				user.setPhone(phone);
				user.setSex(sex);
				user.setUsername(username);
				user.setYobbo(yobbo);
				if(this.userService.addUser(user)){
					return SUCCESS;
				}
			}
		}catch(Exception ex){
			ex.printStackTrace();
			this.addActionMessage("操作失败，请检查重新操作");
		}
		return SUCCESS;
	}
	/**
	 * 编辑预处理
	 * @return
	 */
	public String updatePre(){
		try{
			//获得参数
			UserInfo user = this.userService.loadUserById(username);
			//为页面属性赋值
			this.loginType = user.getLoginType();
			this.phone = user.getPhone();
			this.sex = user.getSex();
			this.username = user.getUsername();
			this.yobbo = user.getYobbo();
		}catch(Exception ex){
			ex.printStackTrace();
			this.addActionMessage("操作失败，请检查重新操作");
		}
		return SUCCESS;
	}
	
	public String updatePass(){
		try{
			UserInfo user = (UserInfo) this.getSession().get("user");
			user.setPassword(password);
			userService.updateUser(user);
			this.addActionMessage("登录密码修改成功,下次登录时生效.");
		}catch(Exception e){
			e.printStackTrace();
			this.addActionMessage(e.getMessage());
		}
		return SUCCESS;
	}
	
	/**
	 * 修改
	 * @return
	 */
	public String update() {
		try {
			// 重新获取修改目标
			UserInfo user = this.userService.loadUserById(username);
			// 修改用户属性
			user.setLoginType(loginType);
			user.setPhone(phone);
			user.setSex(sex);
			user.setYobbo(yobbo);
			// 判断是否需要修改密码
			boolean flag = false;
			if (password != null && !password.equals("")) {// 修改密码
				user.setPassword(password);
				flag = true;
			}
			if (userService.updateUser(user) && flag) {
				this.addActionMessage("修改成功，登录密码在下次登录时生效.");
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			this.addActionMessage(ex.getMessage());
		}
		return SUCCESS;
	}
	/**
	 * 删除
	 * @return
	 */
	public String delete(){
		try{
			UserInfo user = (UserInfo) this.getSession().get("user");
			if(!userService.deleteUser(deleteIds,user)){
				this.addActionMessage("该用户已登录，不可删除");
				this.loadAll();
				return INPUT;
			}
		}catch(Exception ex){
			ex.printStackTrace();
			this.addActionMessage("操作失败，请检查重新操作");
		}
		return SUCCESS;
	}
	/**
	 * 修改密码预处理
	 */
	public String updatePassPre(){
		try{
			String usernameTop = this.getRequest().getParameter("username");
			this.username = usernameTop;
		}catch(Exception ex){
			ex.printStackTrace();
			this.addActionMessage("操作失败，请检查重新操作");
		}
		return SUCCESS;
	}
	/**
	 * get set
	 * @return
	 */
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public Integer getSex() {
		return sex;
	}
	public void setSex(Integer sex) {
		this.sex = sex;
	}
	public String getYobbo() {
		return yobbo;
	}
	public void setYobbo(String yobbo) {
		this.yobbo = yobbo;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		System.out.println(phone);
		this.phone = phone;
	}
	public Integer getLoginType() {
		return loginType;
	}
	public void setLoginType(Integer loginType) {
		this.loginType = loginType;
	}
	public Date getLastLogin() {
		return lastLogin;
	}
	public void setLastLogin(Date lastLogin) {
		this.lastLogin = lastLogin;
	}
	public UserService getUserService() {
		return userService;
	}
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	public List<UserInfo> getUsers() {
		return users;
	}
	public void setUsers(List<UserInfo> users) {
		this.users = users;
	}
	public String getDeleteIds() {
		return deleteIds;
	}
	public void setDeleteIds(String deleteIds) {
		this.deleteIds = deleteIds;
	}
	public Integer getPageCounts() {
		return pageCounts;
	}
	public void setPageCounts(Integer pageCounts) {
		this.pageCounts = pageCounts;
	}
	public String getCurrPageStr() {
		return currPageStr;
	}
	public void setCurrPageStr(String currPageStr) {
		this.currPageStr = currPageStr;
	}
	public Integer getTotalCounts() {
		return totalCounts;
	}
	public void setTotalCounts(Integer totalCounts) {
		this.totalCounts = totalCounts;
	}
}
